//
//  SCWorkoutDataBlock.m
//  FirstSCAnalyzer
//
//  Created by Roelant Ossewaarde on 8/28/09.
//  Copyright 2009 SUNY Buffalo. All rights reserved.
//

#import "SCWorkoutDataBlock.h"


@implementation SCWorkoutDataBlock

@synthesize time;
@synthesize distance;
@synthesize averageStrokeRate;
@synthesize numberOfStrokes;
@synthesize heartRate;
@synthesize intervalType;
@synthesize unitType;
@synthesize date;
@synthesize label;
@synthesize	sequenceno;
@synthesize csvParen;
@synthesize csvSeparator;

-(id) init {
	self = [super init];
	self.distance = 0;
	self.time = 0; // = 4 minutes
	self.averageStrokeRate = 0;
	self.numberOfStrokes = 0;
	self.intervalType = DistanceIntervalType;
	self.label = nil;
	self.date = nil;
	[self setSequenceno:-1];
	[self setCsvParen:@"\""];
	[self setCsvSeparator:@","];
	return self;
}

-(id) initWithLabel:(NSString *)myLabel {
	[self init];
	self.label = myLabel;
	return self;
}

-(void) setWorkoutData:(Chunk *)chunk {
	if ([chunk typeOfChunk] == AutomemWorkoutChunkType) {
		[self setDistance:[chunk getDistance]];
		[self setTime:[chunk getTime]];
		[self setAverageStrokeRate:[chunk getAverageStrokeRate]];
		[self setNumberOfStrokes:[chunk getNumberOfStrokes]];
		//[self setIntervalType:[chunk getIntervalType]];
		
	}
}
	

-(void) logDump {
	NSLog(@"Logdump of workout [%@]: ", [self label]);
	NSLog(@"sequence no: %i", [self sequenceno]);
	NSLog(@"distance: %@", [self distance]);
	NSLog(@"time: %i", [self time]);
	NSLog(@"averageStrokeRate: %i", [self averageStrokeRate]);
	NSLog(@"numberOfStrokes: %i", [self numberOfStrokes]);
	NSLog(@"intervalType: %i", [self intervalType]);
}

-(NSString *) intToCsv:(int) num {
	return [NSString stringWithFormat:@"%@%i%@", [self csvParen], num, [self csvParen]];
}

-(NSString *) floatToCsv:(float) num {
	return [NSString stringWithFormat:@"%@%.2f%@", [self csvParen], num, [self csvParen]];
}

-(NSString *) dateToCsv:(NSDate *) thedate {
	return [NSString stringWithFormat:@"%@%@%@", [self csvParen], [date description], [self csvParen]];
}

-(NSString *) nsstringToCsv:(NSString *) str {
	return [NSString stringWithFormat:@"%@%@%@", [self csvParen], str, [self csvParen]];
}

-(NSString *) csvHeader {
	NSString *sep = [self csvSeparator];
	NSString *headerString = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@%@%@%@\n", 
							  [self nsstringToCsv:@"sequence"], 
							  sep,  
							  [self nsstringToCsv:@"date"], 
							  sep,  
							  [self nsstringToCsv:@"distance"], 
							  sep,  
							  [self nsstringToCsv:@"time"], 
							  sep,  
							  [self nsstringToCsv:@"number of strokes"], 
							  sep,  
							  [self nsstringToCsv:@"strokerate"]
							  ];
	return headerString;
}


-(NSString *) toCSV {
	NSString *sep = [self csvSeparator];

	NSString *headerString = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@%@%@%@\n", 
							  [self intToCsv:[self sequenceno]], 
							  sep,  
							  [self dateToCsv:[self date]], 
							  sep,  
							  [self floatToCsv:(float)[self distance]/16], 
							  sep,  
							  [self floatToCsv:(float)[self time]/16], 
							  sep,  
							  [self intToCsv:[self numberOfStrokes]], 
							  sep,  
							  [self floatToCsv:(float)[self averageStrokeRate]/256]
							  ];
	return headerString;
}
@end
